﻿using Org.Apache.Rocketmq;
using rocketmq.test;
using System.Text;

namespace FifoProducer
{
    public class ProducerFifoMessageExample
    {
        public static async Task QuickStart()
        {
            var clientConfig = RocketMQHelper.GetRocketMQClientConfig();
            //await SendMessage(clientConfig, msg: "hello test1 msg!", topic: "test_event", messageGroup: "pg_event");
            //await SendMessage(clientConfig, msg: "hello test2 msg!", topic: "test_shipment", messageGroup: "pg_shipment");
            await SendMessage(clientConfig, msg: "hello test3 msg!", topic: "test_shipment", messageGroup: "test_group_event");
        }

        private static async Task SendMessage(ClientConfig clientConfig, string msg, string topic, string messageGroup)
        {
            var producer = await new Producer.Builder()
                .SetTopics(topic)
                .SetClientConfig(clientConfig)
                .Build();

            // Define your message body.
            for (int i = 0; i < 10; i++)
            {
                var strMessage = $"{msg} {i}";
                var bytes = Encoding.UTF8.GetBytes(strMessage);
                var message = new Message.Builder()
                    .SetTopic(topic)
                    .SetBody(bytes)
                    .SetMessageGroup(messageGroup)
                    .Build();
                var sendReceipt = await producer.Send(message);
                await Console.Out.WriteLineAsync($"Send topic:[{topic}],messageGroup:[{messageGroup}], sendReceipt={sendReceipt}, message:{strMessage}");
            }
            // Close the producer if you don't need it anymore.
            await producer.DisposeAsync();
            await Console.Out.WriteLineAsync("Send Success!");
        }
    }
}
